package com.jiutianjia.cloud.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * swagger配置，生产不开启，dev sit 环境开启
 *
 * @link <a href="http://localhost:8001/goods/doc.html#/home">...</a>
 */
@Configuration
@EnableKnife4j
@EnableOpenApi
@ConditionalOnProperty(value = "spring.profiles.active", havingValue = "dev")
public class SwaggerConfig {

    @Bean
    public Docket createRestApis() {
        return new Docket(DocumentationType.OAS_30).enable(true)//是否启用：注意生产环境需要关闭
                .groupName("spring-boot-2.7.0").genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false).forCodeGeneration(true).ignoredParameterTypes(CookieValue.class).apiInfo(apiInfo()).select()
                //以下拦截配置可以三选一，根据需要进行添加
                .apis(RequestHandlerSelectors.basePackage("com.jiutianjia.cloud.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("使用swagger生成的接口文档").description("开发测试")
                // 服务条款URL
                .termsOfServiceUrl("https://www.baidu.com/").build();
    }


}
